/*	Copyright (C) 2014  Ilchenko Vladuslav Oleksiyovich

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or any
	later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <string>
#include <random>
#include <strstream>
#include <chrono>
#include <cmath>
#include <NTL\ZZ.h>
using namespace NTL;
using namespace std;
using namespace chrono;

#pragma comment(lib,"NTL.lib")

class RSAKey
{
public:
	string e;
	string d;
	string n;
	RSAKey(){}
	~RSAKey(){}
	void SavePublicKeyToFile(string &dir);
	void SavePrivateKeyToFile(string &dir);
	void GetPublicKeyForFile(string &dir);
	void GetPrivateKeyForFile(string &dir);
	string GetPublicKey();
	string GetPrivateKey();
};

class RSA
{
public:
	RSA();
	~RSA();
	int RSAKeyGen(RSAKey &key,long lbits);
	void Dec_To_Hex(ZZ dec,string &str);
	void Hex_To_Dec(string &str,ZZ &dec);
	string Str_To_Base64(string str);
	string Base64_To_Str(string b64);
	void StrToZZ(string &LP_txt,ZZ &LP_Num);
	void ZZToStr(ZZ &LP_Num,string &LP_Txt);
	ZZ EncodeString(RSAKey key,string text);
	string DecodeString(RSAKey key,ZZ cript);
private:
	string base64_chars;
	void Encode(ZZ &to_enc,ZZ e,ZZ n);
	void Decode(ZZ &to_dec,ZZ d,ZZ n);
	inline bool is_base64(unsigned char c);
};